﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using HML.Design;

namespace HML
{
    /// <summary>
    ///内容节点集合
    /// </summary>
    [Editor(typeof(SingleAccordionNodeCollectionEditor), typeof(UITypeEditor))]
    public class SingleAccordionContentNodeCollection : IList, ICollection, IEnumerable
    {
        #region

        private ArrayList singleAccordionContentNodeList = new ArrayList();

        private SingleAccordionCategoryNode category = null;

        #endregion

        internal SingleAccordionContentNodeCollection(SingleAccordionCategoryNode category)
        {
            this.category = category;
        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            SingleAccordionContentNode[] listArray = new SingleAccordionContentNode[this.singleAccordionContentNodeList.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (SingleAccordionContentNode)this.singleAccordionContentNodeList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.singleAccordionContentNodeList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.singleAccordionContentNodeList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is SingleAccordionContentNode))
            {
                throw new ArgumentException("类型应该为 SingleAccordionContentNode");
            }

            return this.Add((SingleAccordionContentNode)value);
        }

        public int Add(SingleAccordionContentNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (node.Owner != null || node.Parent != null)
            {
                throw new ArgumentException("SingleAccordionContentNode节点不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            node.Owner = null;
            node.Parent = this.category;
            this.singleAccordionContentNodeList.Add(node);
            if (this.category.Owner != null)
            {
                this.category.Owner.UpdateRealityRectangle();
                this.category.Owner.UpdateAllNodeRectangle();
                this.category.Owner.UpdateScrollRectangle();
                this.category.Owner.Invalidate();
            }
            return this.Count - 1;
        }

        public void Clear()
        {
            for (int i = 0; i < this.singleAccordionContentNodeList.Count; i++)
            {
                SingleAccordionContentNode node = (SingleAccordionContentNode)this.singleAccordionContentNodeList[i];
                node.Owner = null;
                node.Parent = null;
            }
            this.singleAccordionContentNodeList.Clear();
            if (this.category.Owner != null)
            {
                this.category.Owner.UpdateRealityRectangle();
                this.category.Owner.UpdateAllNodeRectangle();
                this.category.Owner.UpdateScrollRectangle();
                this.category.Owner.Invalidate();
                this.category.Owner.SelectedNode = this.category.Owner.SelectedNode;
            }
        }

        public bool Contains(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object value)
        {
            if (value is SingleAccordionContentNode)
            {
                return this.Contains((SingleAccordionContentNode)value);
            }
            return false;
        }

        public int IndexOf(object value)
        {
            if (value is SingleAccordionContentNode)
            {
                return this.singleAccordionContentNodeList.IndexOf(value);
            }
            return -1;
        }

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="index">插入索引</param>
        /// <param name="value">节点（类型应该为 SingleAccordionContentNode）</param>
        public void Insert(int index, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is SingleAccordionContentNode))
            {
                throw new ArgumentException("类型应该为 SingleAccordionContentNode");
            }

            SingleAccordionContentNode node = (SingleAccordionContentNode)value;
            if (node.Owner != null || node.Parent != null)
            {
                throw new ArgumentException("SingleAccordionContentNode节点不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            if (index > this.Count)
            {
                index = this.Count;
            }
            ArrayList tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                tmp.Add(this.singleAccordionContentNodeList[i]);
            }
            node.Owner = null;
            node.Parent = this.category;
            tmp.Add(value);
            for (int i = index; i < this.Count; i++)
            {
                tmp.Add(this.singleAccordionContentNodeList[i]);
            }
            this.singleAccordionContentNodeList = tmp;
            if (this.category.Owner != null)
            {
                this.category.Owner.UpdateRealityRectangle();
                this.category.Owner.UpdateAllNodeRectangle();
                this.category.Owner.UpdateScrollRectangle();
                this.category.Owner.Invalidate();
            }
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// 移除节点
        /// </summary>
        /// <param name="value">节点（类型应该为 SingleAccordionContentNode）</param>
        public void Remove(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is SingleAccordionContentNode))
            {
                throw new ArgumentException("类型应该为 SingleAccordionContentNode");
            }

            this.Remove((SingleAccordionContentNode)value);
        }

        public void Remove(SingleAccordionContentNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            node.Owner = null;
            node.Parent = null;
            this.singleAccordionContentNodeList.Remove(node);
            if (this.category.Owner != null)
            {
                this.category.Owner.UpdateRealityRectangle();
                this.category.Owner.UpdateAllNodeRectangle();
                this.category.Owner.UpdateScrollRectangle();
                this.category.Owner.Invalidate();
                this.category.Owner.SelectedNode = this.category.Owner.SelectedNode;
            }
        }

        public void RemoveAt(int index)
        {
            SingleAccordionContentNode node = (SingleAccordionContentNode)this.singleAccordionContentNodeList[index];
            node.Owner = null;
            node.Parent = null;
            this.singleAccordionContentNodeList.RemoveAt(index);
            if (this.category.Owner != null)
            {
                this.category.Owner.UpdateRealityRectangle();
                this.category.Owner.UpdateAllNodeRectangle();
                this.category.Owner.UpdateScrollRectangle();
                this.category.Owner.Invalidate();
                this.category.Owner.SelectedNode = this.category.Owner.SelectedNode;
            }
        }

        public SingleAccordionContentNode this[int index]
        {
            get { return (SingleAccordionContentNode)this.singleAccordionContentNodeList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (value.Owner != null || value.Parent != null)
                {
                    throw new ArgumentException("SingleAccordionContentNode节点不应该同时被添加到多个地方，请先从原有地方移除再添加");
                }

                ((SingleAccordionContentNode)this.singleAccordionContentNodeList[index]).Owner = null;
                ((SingleAccordionContentNode)this.singleAccordionContentNodeList[index]).Parent = null;
                value.Owner = null;
                value.Parent = this.category;
                this.singleAccordionContentNodeList[index] = value;
                if (this.category.Owner != null)
                {
                    this.category.Owner.UpdateRealityRectangle();
                    this.category.Owner.UpdateAllNodeRectangle();
                    this.category.Owner.UpdateScrollRectangle();
                    this.category.Owner.Invalidate();
                    this.category.Owner.SelectedNode = this.category.Owner.SelectedNode;
                }
            }
        }

        object IList.this[int index]
        {
            get { return (object)this.singleAccordionContentNodeList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (!(value is SingleAccordionContentNode))
                {
                    throw new ArgumentException("类型应该为 SingleAccordionContentNode");
                }
                SingleAccordionContentNode node = (SingleAccordionContentNode)value;
                if (node.Owner != null || node.Parent != null)
                {
                    throw new ArgumentException("SingleAccordionContentNode节点不应该同时被添加到多个地方，请先从原有地方移除再添加");
                }

                ((SingleAccordionContentNode)this.singleAccordionContentNodeList[index]).Owner = null;
                ((SingleAccordionContentNode)this.singleAccordionContentNodeList[index]).Parent = null;
                node.Owner = null;
                node.Parent = this.category;
                this.singleAccordionContentNodeList[index] = node;
                if (this.category.Owner != null)
                {
                    this.category.Owner.UpdateRealityRectangle();
                    this.category.Owner.UpdateAllNodeRectangle();
                    this.category.Owner.UpdateScrollRectangle();
                    this.category.Owner.Invalidate();
                    this.category.Owner.SelectedNode = this.category.Owner.SelectedNode;
                }
            }
        }

        #endregion

    }

}
